package com.mes.schedule.dao;

import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.stereotype.Repository;

import com.mes.common.dao.BasicDao;
import com.mes.common.exception.db.BaseDBException;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;

import com.mes.schedule.dao.iface.ISPartTaskDao;
import com.mes.schedule.domain.SOperationTask;
import com.mes.schedule.domain.SPartTask;

@Repository
public class SPartTaskDao extends BasicDao<SPartTask> implements ISPartTaskDao{
	private static final Log log = LogFactory.getLog(SPartTaskDao.class);
	
	/**
	 * @see the method use for ScheduleManager
	 * @param scheduleUid
	 * @throws BaseDBException
	 */
	public void deleteAllPartTask(String scheduleUid) throws BaseDBException {
		try {

			Session session = this.getSession();
			Query query = session
					.createQuery("delete SPartTask tsp where tsp.scheduleUid=?");
			query.setString(0, scheduleUid);
			query.executeUpdate();

		} catch (DataAccessResourceFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return;

	}
	
	/**
	 * @see the method use for STaskDao
	 * @param scheduleUid
	 * @param taskUid
	 * @return
	 * @throws BaseDBException
	 */
	public SPartTask getSchedulePartTask(String scheduleUid, String taskUid)
			throws BaseDBException {

		try {
			SPartTask partTask = new SPartTask();

			partTask.setScheduleUid(scheduleUid);
			partTask.setTaskUid(taskUid);
			Session session = this.getSession();

			return (SPartTask) session.get(SPartTask.class, partTask);
			// Criteria crit = session.createCriteria(SPartTask.class);
			// crit.add(Restrictions.eq("scheduleUid", scheduleUid));
			// crit.add(Restrictions.eq("taskUid", taskUid));
			// partTask = (SPartTask) crit.uniqueResult();
		} catch (DataAccessResourceFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @see the method use for STaskDao
	 * @param scheduleUid
	 * @return
	 * @throws BaseDBException
	 */
	public List<SPartTask> getSchedulePartTask(String scheduleUid)
			throws BaseDBException {
		List<SPartTask> list = new ArrayList<SPartTask>();
		try {
			/**
			 * @author SnowBear
			 * session的创建要不要仿照basicDao中的工厂创建
			 */
			Session session = this.getSession();
			Criteria crit = session.createCriteria(SPartTask.class);
			crit.add(Restrictions.eq("scheduleUid", scheduleUid));
			crit.add(Restrictions.eq("taskScheduleState", 1));
			crit.add(Restrictions.lt("taskState", 106));
			crit.addOrder(Property.forName("priority").asc());
			crit.addOrder(Property.forName("plangrade").desc());
			crit.addOrder(Property.forName("lateFinish").asc());
			list = crit.list();
			// Criteria crit = session.createCriteria(SPartTask.class);
			// crit.add(Restrictions.eq("scheduleUid", scheduleUid));
			// crit.add(Restrictions.eq("taskUid", taskUid));
			// partTask = (SPartTask) crit.uniqueResult();
		} catch (DataAccessResourceFailureException e) {
			e.printStackTrace();
		} catch (HibernateException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}
		return list;
	}
}
